﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace test
{
    public class ConfigSection
    {
        static bool IsLoad = false;
        static ConfigSection()
        {
            Load();
        }
        public static XDocument Xdoc
        {
            set;
            get;
        }

        public static List<ConfigInfo> ConfigInfoList = new List<ConfigInfo>();

        public static string filePath = AppDomain.CurrentDomain.BaseDirectory + "/Config/GlobalConfig.xml";

        public static void Load()
        {
            ConfigureAndWatch(true);
            Load(filePath);
        }

        public static void Load(string path)
        {
            filePath = path;
            XDocument doc = XDocument.Load(filePath);
            Xdoc = doc;

            Init();
            IsLoad = true;
        }

        public static string Get(string key)
        {
            if (!IsLoad)
            {
                Load();
            }
            ConfigInfo info = ConfigInfoList.Find(t => t.Key.ToLower() == key.ToLower());

            return info == null ? "" : info.Value;
        }

        public static int GetCount()
        {
            return ConfigInfoList.Count;
        }

        public static string GetAllKeyValue()
        {
            var result = string.Empty;
            if (!IsLoad)
            {
                Load();
            }
            foreach (var item in ConfigInfoList)
            {
                result += "[" + item.Key + "]" + item.Value;
            }
            return result;
        }

        private static void Init()
        {
            ConfigInfoList.Clear();

            List<XElement> xes = Xdoc.Descendants("item").ToList();

            ConfigInfo info = null;

            foreach (XElement node in xes)
            {
                info = new ConfigInfo()
                {
                    Key = node.Attribute("key").Value,
                    Value = node.Value
                };
                ConfigInfoList.Add(info);
            }

        }

        static FileSystemWatcher watch;
        private static void ConfigureAndWatch(bool isWatch = false)
        {
            string filePath = ConfigSection.filePath;
            //监视文件的改动
            if (File.Exists(filePath) && isWatch)
            {
                watch = new FileSystemWatcher();
                watch.BeginInit();
                watch.Path = new FileInfo(filePath).DirectoryName;
                watch.Changed += new FileSystemEventHandler(watch_Changed);
                watch.EnableRaisingEvents = true;
                watch.EndInit();
            }
        }

        static void watch_Changed(object sender, FileSystemEventArgs e)
        {
            ConfigSection.Load(filePath);
        }
    }
    public class ConfigInfo
    {
        public string Key;
        public string Value;
    }
}
